﻿using SqlSugar;
using System;
using System.Collections.Generic;

namespace WPFNote.Models.Utility
{
    public static class MySqlWhere
    {
        /// <summary>
        /// 生成SQL语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static (string, SugarParameter[]) Sql<T>(T t)
        {
            const string suffix = "_ts";
            var param = new List<SugarParameter>();
            var sql = "";
            var type = t.GetType();
            foreach (var item in type.GetProperties())
            {
                var name = item.Name;
                var value = item.GetValue(t, null);
                if (value != null)
                {
                    var v = (MySqlAttribute[]) item.GetCustomAttributes(typeof(MySqlAttribute), false);
                    var descriptionName = v.Length > 0 ? v[0].Description : "";
                    var paramName = name + suffix;
                    if (!string.IsNullOrEmpty(descriptionName))
                    {
                        switch (descriptionName)
                        {
                            case "SqlLike":
                                sql += name + " like @" + paramName + " and ";
                                param.Add(new SugarParameter("@" + paramName, "%" + value + "%"));
                                break;
                            case "Start":
                                sql += name.Replace("Start", "") + ">=@" + paramName + " and ";
                                param.Add(new SugarParameter("@" + paramName, value));
                                break;
                            case "End":
                                sql += name.Replace("End", "") + "<@" + paramName + " and ";
                                param.Add(new SugarParameter("@" + paramName,
                                    Convert.ToDateTime(value).AddDays(1)));
                                break;
                            case "Ignore":
                                break;
                            default:
                                sql += name + "=@" + paramName + " and ";
                                param.Add(new SugarParameter("@" + paramName, value));
                                break;
                        }
                    }
                    else
                    {
                        sql += name + "=@" + paramName + " and ";
                        param.Add(new SugarParameter("@" + paramName, value));
                    }
                }
            }

            sql = string.IsNullOrWhiteSpace(sql) ? "" : (sql.Length > 5 ? sql[..^5] : sql);
            param = string.IsNullOrWhiteSpace(sql) ? new List<SugarParameter>() : param;
            return (sql, param.ToArray());
        }

        public static string Columns<T>(T t)
        {
            var res = new List<string>();
            var type = t.GetType();
            foreach (var item in type.GetProperties())
            {
                var name = item.Name;
                var value = item.GetValue(t, null);
                if (value != null)
                {
                    var v = (MySqlAttribute[]) item.GetCustomAttributes(typeof(MySqlAttribute), false);
                    var descriptionName = v.Length > 0 ? v[0].Description : "";
                    if (!string.IsNullOrEmpty(descriptionName))
                    {
                        if (!descriptionName.Equals("Ignore"))
                        {
                            res.Add(name);
                        }
                    }
                    else
                    {
                        res.Add(name);
                    }
                }
            }

            return string.Join(",", res);
        }
    }
}